package com.hotelManager.www.dao;

import com.hotelManager.www.po.UserBlacklist;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * UserBlacklist DAO 类
 * 实现用户黑名单的数据库操作，适应无外键的情况。
 */
public class MerchantUserBlacklistDao {

    /**
     * 添加用户到黑名单
     * 手动验证 userId 和 blacklistedUserId 的有效性。
     * @param conn 数据库连接
     * @param blacklist 黑名单记录
     * @return 插入是否成功
     * @throws SQLException 如果数据库操作失败
     */
    public boolean addToBlacklist(Connection conn, UserBlacklist blacklist) throws SQLException {
        // 验证 userId 是否存在
        String userCheckSql = "SELECT COUNT(*) FROM users WHERE id = ?";
        try (PreparedStatement userCheckStmt = conn.prepareStatement(userCheckSql)) {
            userCheckStmt.setInt(1, blacklist.getUserId());
            try (ResultSet rs = userCheckStmt.executeQuery()) {
                if (rs.next() && rs.getInt(1) == 0) {
                    throw new SQLException("用户ID不存在: " + blacklist.getUserId());
                }
            }
        }

        // 插入黑名单记录
        String sql = "INSERT INTO user_blacklist (shop_id, user_id, created_at) VALUES (?, ?, ?)";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, blacklist.getShopId());
            ps.setInt(2, blacklist.getUserId());
            ps.setTimestamp(3, blacklist.getCreatedAt());
            return ps.executeUpdate() > 0;
        }
    }

    /**
     * 从黑名单中移除用户
     * @param conn 数据库连接
     * @param userId 用户ID
     * @param blacklistedUserId 被拉黑的用户ID
     * @return 删除是否成功
     * @throws SQLException 如果数据库操作失败
     */
    public boolean removeFromBlacklist(Connection conn, int userId, int blacklistedUserId) throws SQLException {
        String sql = "DELETE FROM user_blacklist WHERE user_id = ? AND blacklisted_user_id = ?";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            ps.setInt(2, blacklistedUserId);
            return ps.executeUpdate() > 0;
        }
    }

    /**
     * 检查用户是否在黑名单中
     * @param conn 数据库连接
     * @param userId 用户ID
     * @param blacklistedUserId 被拉黑的用户ID
     * @return 是否在黑名单中
     * @throws SQLException 如果数据库操作失败
     */
    public boolean isUserBlacklisted(Connection conn, int userId, int blacklistedUserId) throws SQLException {
        String sql = "SELECT COUNT(*) FROM user_blacklist WHERE user_id = ? AND blacklisted_user_id = ?";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            ps.setInt(2, blacklistedUserId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        }
        return false;
    }

    /**
     * 获取用户的黑名单列表
     * @param conn 数据库连接
     * @param userId 用户ID
     * @return 黑名单列表
     * @throws SQLException 如果数据库操作失败
     */
    public List<UserBlacklist> getUserBlacklist(Connection conn, int userId) throws SQLException {
        String sql = "SELECT * FROM user_blacklist WHERE user_id = ? ORDER BY created_time DESC";
        List<UserBlacklist> blacklist = new ArrayList<>();
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    UserBlacklist item = new UserBlacklist();
                    item.setId(rs.getInt("id"));
                    item.setUserId(rs.getInt("user_id"));
                    item.setCreatedAt(rs.getTimestamp("created_time"));
                    blacklist.add(item);
                }
            }
        }
        return blacklist;
    }

    /**
     * 添加用户到黑名单
     * @param userBlacklist 黑名单记录
     * @return 插入是否成功
     */
    public boolean addUserToBlacklist(Connection conn,UserBlacklist userBlacklist) {
        String sql = "INSERT INTO user_blacklist (shop_id, user_id, reason, created_time) VALUES (?, ?, ?, ?)";
        try (
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userBlacklist.getShopId());
            ps.setInt(2, userBlacklist.getUserId());
            ps.setTimestamp(4, userBlacklist.getCreatedAt());
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取商家的黑名单列表
     * @param shopId 商家ID
     * @return 黑名单列表
     */
    public List<UserBlacklist> getBlacklistByShopId(Connection conn,Integer shopId) {
        String sql = "SELECT * FROM user_blacklist WHERE shop_id = ? ORDER BY created_time DESC";
        List<UserBlacklist> blacklist = new ArrayList<>();
        try (
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, shopId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    UserBlacklist userBlacklist = new UserBlacklist();
                    userBlacklist.setId(rs.getInt("id"));
                    userBlacklist.setShopId(rs.getInt("shop_id"));
                    userBlacklist.setUserId(rs.getInt("user_id"));
                    userBlacklist.setCreatedAt(rs.getTimestamp("created_time"));
                    blacklist.add(userBlacklist);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return blacklist;
    }

    /**
     * 从黑名单中移除用户
     * @param id 黑名单记录ID
     * @return 删除是否成功
     */
    public boolean removeUserFromBlacklist(Connection conn,Integer id) {
        String sql = "DELETE FROM user_blacklist WHERE id = ?";
        try (
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}